<template>
  <Card dis-hover :bordered="false">
    <Form inline>
      <Row>
        <Col span="22">
          <FormItem>
            <DatePicker
              placeholder="创建日期选择"
              type="daterange"
              :options="options"
              v-model="params.date"
              @on-change="searchData"
              clearable
            />
          </FormItem>
          <FormItem>
            <Input
              placeholder="请输入昵称"
              v-model="params.keywords"
              @on-clear="searchData"
              @on-enter="searchData"
              clearable
            />
          </FormItem>
          <FormItem>
            <Select
              v-width="120"
              placeholder="请选择平台"
              :filterable="platList.length >= 10"
              v-model="params.platform_id"
              @on-change="searchData"
              clearable
            >
              <Option
                v-for="item in platList"
                :value="item.value"
                :key="item.value"
                >{{ item.label }}</Option
              >
            </Select>
          </FormItem>
          <FormItem>
            <Select
              v-width="120"
              placeholder="账号类型"
              :filterable="countList.length >= 10"
              v-model="params.t_id"
              @on-change="searchData"
              clearable
            >
              <Option
                v-for="item in countList"
                :value="item.value"
                :key="item.value"
                >{{ item.label }}</Option
              >
            </Select>
          </FormItem>
          <FormItem>
            <Select
              v-width="120"
              placeholder="请选择创建人"
              :filterable="userList.length >= 10"
              v-model="params.created_member_id"
              @on-change="searchData"
              clearable
            >
              <Option
                v-for="item in userList"
                :value="item.value"
                :key="item.value"
                >{{ item.label }}</Option
              >
            </Select>
          </FormItem>
        </Col>
        <Col span="2" style="text-align: right">
          <Button
            type="primary"
            @click="handleAdd"
            class="ivu-ml-8"
            v-hasC="{
              name: 'v1-buss-target-accounts-update',
              flag: 'v1/buss/target-accounts/update',
            }"
            >新增</Button
          >
        </Col>
      </Row>
    </Form>
    <Table
      :columns="columns"
      :data="list"
      :loading="loading"
      @on-sort-change="sortChange"
      :max-height="tableHeigth"
    >
      <template #status="{ row }">
        <Badge status="success" text="正常" v-if="row.status === '1'" />
        <Badge status="error" text="停用" v-else />
      </template>
      <template #action="{ row }">
        <a
          @click="handleEdit(row)"
          v-hasC="{
            name: 'v1-buss-target-accounts-update2',
            flag: 'v1/buss/target-accounts/update',
          }"
          >编辑</a
        >
        <Divider
          type="vertical"
          v-hasC="{
            name: 'v1-buss-target-accounts-update2',
            flag: 'v1/buss/target-accounts/update',
          }"
        />
        <a
          @click="handleDel(row.id)"
          class="del"
          v-hasC="{
            name: 'v1-buss-target-accounts-delete',
            flag: 'v1/buss/target-accounts/delete',
          }"
          >删除</a
        >
      </template>
    </Table>
    <Page
      class="ivu-mt-16"
      :total="total"
      :modelValue="params.current"
      :page-size="params.size"
      :page-size-opts="pageOpts"
      show-total
      show-sizer
      @on-change="changePage"
      @on-page-size-change="changepageSize"
    />
    <Modal
      :mask-closable="false"
      v-model="addModal"
      :title="edit ? '新增' : '编辑'"
      :footer-hide="true"
      :draggable="true"
    >
      <Form ref="create" :model="acountInfo" :rules="rules" :label-width="80">
        <FormItem label="系统账号" prop="accounts_id">
          <Select
            v-model="acountInfo.accounts_id"
            :filterable="systemAddList.length >= 10"
            clearable
          >
            <Option v-for="item in systemAddList" :value="+item.value">{{
              item.label
            }}</Option>
          </Select>
        </FormItem>
        <FormItem label="对标账号" prop="name" required>
          <Input
            v-model="acountInfo.name"
            placeholder="对标账号"
            :maxlength="30"
          />
        </FormItem>
        <FormItem label="账号类型" prop="t_id" required>
          <Select
            v-model="acountInfo.t_id"
            placeholder="账号类型"
            :filterable="countAddList.length >= 10"
            clearable
          >
            <Option
              v-for="item in countAddList"
              :value="+item.value"
              :key="item.value"
              >{{ item.label }}</Option
            >
          </Select>
        </FormItem>
        <FormItem label="人设定位" prop="position">
          <Input
            v-model="acountInfo.position"
            placeholder="人设定位"
            :maxlength="30"
          />
        </FormItem>
        <FormItem label="平台" prop="platform_id" required>
          <Select
            v-model="acountInfo.platform_id"
            placeholder="平台"
            :filterable="platAddList.length >= 10"
            clearable
          >
            <Option
              v-for="item in platAddList"
              :value="+item.value"
              :key="item.value"
              >{{ item.label }}</Option
            >
          </Select>
        </FormItem>
        <FormItem label="粉丝数" prop="fans_num" required>
          <InputNumber
            v-model="acountInfo.fans_num"
            :min="0"
            placeholder="粉丝数"
          />
        </FormItem>
        <FormItem label="作品数" prop="works_num" required>
          <InputNumber
            v-model="acountInfo.works_num"
            :min="0"
            placeholder="作品数"
          />
        </FormItem>
        <FormItem label="状态" prop="status">
          <Switch v-model="acountInfo.status" />
        </FormItem>
      </Form>
      <div slot="footer" style="text-align: right">
        <Button type="default" @click="addModal = false">取消</Button>
        <Button
          type="primary"
          @click="ok"
          :loading="btnLoading"
          style="margin-left: 10px"
          >确定</Button
        >
      </div>
    </Modal>
  </Card>
</template>
<script>
import dayjs from "dayjs";
import {
  targetAccountsList,
  createTargetAccounts,
  deleteTargetAccounts,
  targetCondition,
} from "@api/targetaccounts.js";
export default {
  name: "benchmarkingaccount",
  data() {
    return {
      tableHeigth: document.body.clientHeight - 280,
      addModal: false,
      edit: true,
      params: {
        date: [],
        keywords: "",
        platform_id: "",
        t_id: "",
        status: "",
        created_member_id: "",
        current: 1,
        size: 10,
      },
      platList: [],
      countList: [],
      userList: [],
      systemAddList: [],
      countAddList: [],
      platAddList: [],
      total: 0,
      pageOpts: [10, 20, 30, 40],
      columns: [
        {
          key: "id",
          title: "ID",
          width: 60,
        },
        {
          key: "accounts_name",
          title: "系统账号",
          ellipsis: "true",
          tooltip: "true",
          minWidth: 180,
        },
        {
          key: "name",
          title: "对标账号",
          ellipsis: "true",
          tooltip: "true",
          minWidth: 120,
        },
        {
          key: "t_name",
          title: "账号类型",
          ellipsis: "true",
          tooltip: "true",
          minWidth: 100,
        },
        {
          key: "position",
          title: "人设定位",
          ellipsis: "true",
          tooltip: "true",
          minWidth: 100,
        },
        {
          key: "platform_name",
          title: "平台",
          ellipsis: "true",
          tooltip: "true",
          minWidth: 100,
        },
        {
          key: "fans_num",
          title: "粉丝数",
          width: 100,
          sortable: "custom",
        },
        {
          key: "works_num",
          title: "发布作品数",
          width: 120,
          sortable: "custom",
        },
        {
          slot: "status",
          title: "运营状态",
          width: 80,
        },
        {
          key: "created_member_name",
          title: "创建人",
          width: 100,
        },
        {
          key: "created_at_str",
          title: "创建日期",
          width: 170,
        },
        {
          key: "change_date",
          title: "更新日期",
          width: 170,
        },
        {
          slot: "action",
          title: "操作",
          width: 110,
          fixed: "right",
        },
      ],
      list: [],
      loading: false,
      btnLoading: false,
      acountInfo: {
        id: "",
        accounts_id: "",
        name: "",
        t_id: "",
        position: "",
        platform_id: "",
        fans_num: null,
        works_num: null,
        status: true,
      },
      rules: {
        name: [{ required: true, message: "请输入对标账号" }],
        t_id: [{ required: true, message: "请选择账号类型" }],
        platform_id: [{ required: true, message: "请选择平台" }],
        fans_num: [{ required: true, message: "请输入粉丝数" }],
        works_num: [{ required: true, message: "请输入作品数" }],
      },
      options: {
        disabledDate(date) {
          return date && date.valueOf() > Date.now();
        },
      },
    };
  },
  computed: {},
  mounted() {
    this.getData();
  },
  methods: {
    sortChange(e) {
      this.params.order_column = e.key.replace(/_str$/, "");
      this.params.order_sort = e.order;
      this.getData();
    },
    //切换page
    changePage(index) {
      this.params.current = index;
      this.getData();
    },
    changepageSize(val) {
      this.params.size = val;
      this.getData();
    },
    handleDel(index) {
      this.$Modal.confirm({
        title: "删除提示",
        content: "是否确认删除此条记录，删除后不可恢复",
        onOk: () => {
          deleteTargetAccounts({ id: index }).then(() => {
            this.$Message.success("删除成功");
            this.getData();
          });
        },
      });
    },
    handleAdd() {
      targetCondition({ id: 0 }).then((res) => {
        this.systemAddList = res.condition.account_arr;
        this.countAddList = res.condition.account_type_arr;
        this.platAddList = res.condition.platform_arr;
        this.acountInfo = {
          accounts_id: "",
          name: "",
          t_id: "",
          position: "",
          platform_id: "",
          fans_num: null,
          works_num: null,
          status: true,
        };
      });

      this.addModal = true;
      this.edit = true;
      this.$refs.create.resetFields();
    },
    handleEdit(info) {
      this.addModal = true;
      this.edit = false;
      targetCondition({ id: info.id }).then((res) => {
        this.systemAddList = res.condition.account_arr;
        this.countAddList = res.condition.account_type_arr;
        this.platAddList = res.condition.platform_arr;
        this.acountInfo = {
          id: info.id,
          accounts_id: +info.accounts_id,
          name: info.name,
          t_id: +info.t_id,
          position: info.position,
          platform_id: +info.platform_id,
          fans_num: +info.fans_num,
          works_num: +info.works_num,
          status: info.status == 1 ? true : false,
        };
      });
    },
    getData() {
      this.loading = true;
      const params = {
        created_stime: this.params.date[0]
          ? dayjs(this.params.date[0]).format("YYYY-MM-DD")
          : "",
        created_etime: this.params.date[1]
          ? dayjs(this.params.date[1]).format("YYYY-MM-DD")
          : "",
        keywords: this.params.keywords,
        platform_id: this.params.platform_id,
        t_id: this.params.t_id,
        status: this.params.status,
        created_member_id: this.params.created_member_id,
        pageSize: this.params.size,
        page: this.params.current,
      };
      targetAccountsList(params)
        .then((res) => {
          this.countList = res.condition.account_type_arr;
          this.userList = res.condition.created_member_arr;
          this.platList = res.condition.platform_arr;
          this.list = res.list;
          this.total = res.total;
          this.loading = false;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    searchData() {
      this.params.current = 1;
      this.getData();
    },
    addAcount() {
      this.btnLoading = true;
      const params = {
        id: this.acountInfo.id,
        accounts_id: this.acountInfo.accounts_id,
        name: this.acountInfo.name,
        t_id: this.acountInfo.t_id,
        position: this.acountInfo.position,
        platform_id: this.acountInfo.platform_id,
        fans_num: this.acountInfo.fans_num,
        works_num: this.acountInfo.works_num,
        status: this.acountInfo.status ? 1 : 0,
      };
      if (this.edit) {
        createTargetAccounts(params)
          .then((res) => {
            this.$Message.success("新增成功");
            this.addModal = false;
            this.btnLoading = false;
            this.getData();
          })
          .catch(() => {
            this.btnLoading = false;
          });
      } else {
        createTargetAccounts(
          Object.assign({}, params, { id: this.acountInfo.id })
        )
          .then((res) => {
            this.$Message.success("编辑成功");
            this.addModal = false;
            this.btnLoading = false;
            this.getData();
          })
          .catch(() => {
            this.btnLoading = false;
          });
      }
    },
    ok() {
      this.$refs.create.validate((valid) => {
        if (valid) {
          this.addAcount();
        } else {
          this.addModal = true;
          return;
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
/deep/.ivu-table-cell {
  word-break: keep-all;
}
</style>
